using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.PageControls;
using Kugar.UI.Web.AdminLTE.PageControls.PageCommon;
using Kugar.UI.Web.AdminLTE.PageControls.Helpers.ButtonExtMethod;
using Kugar.UI.Web.AdminLTE.PageControls.PageForms;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_TreeViewSelector:PageForm_SingleInputFieldBase
    {
        private bool _disabled;
        private PageCommon_Tree _tree=null;
        private IEnumerable<string> _values = null;


        public PageForm_TreeViewSelector(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_treeviewselectorfield");
            _tree=new PageCommon_Tree(viewContext);
            _tree.Css("dropdown-menu dropdown-treeview-menu-container");
        }

        public PageForm_TreeViewSelector  Name(string name)
        {
            _name = name;

            return this;
        }

        /// <summary>
        /// 修改时,触发的事件处理语句
        /// </summary>
        /// <param name="changedScript"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector OnChangedScript(string changedScript)
        {
            _tree.OnChangedScript(changedScript);

            return this;
        }

        /// <summary>
        /// 点击节点时,触发的事件处理语句
        /// </summary>
        /// <param name="changedScript"></param>
        /// <returns></returns>

        public PageForm_TreeViewSelector OnItemClickCallback(Func<SelectorItem,string> onClickScript)
        {
            _tree.OnItemClickCallback(onClickScript);
            return this;
        }

        public PageForm_TreeViewSelector OnItemClickCallback(string onClickScript)
        {
            _tree.OnItemClickCallback((x)=>onClickScript);
            return this;
        }

        /// <summary>
        /// 是否允许多选
        /// </summary>
        /// <param name="isMultiSelect"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector MultiSelect(bool isMultiSelect=true)
        {
            _tree.MultiSelect(isMultiSelect);
            return this;
        }

        /// <summary>
        /// 选中的节点Value值(多个)
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector SelectedValues(IEnumerable<string> values)
        {
            _values = values;
            _tree.SelectedValues(values);
            return this;
        }

        /// <summary>
        /// 选中的节点Value值(单个)
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector SelectedValues(string values)
        {
            _values = new []{values};
            _tree.SelectedValues(_values);
            return this;
        }

        /// <summary>
        /// 获取指定父节点的直接下级直接点,如果为空,可返回null或者空数组
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector Children(Func<SelectorItem, IEnumerable<(string title,string value)>> childrenFunc)
        {
            _tree.Children(childrenFunc);
            return this;
        }

        /// <summary>
        /// 用于获取下级数据源的函数
        /// </summary>
        /// <param name="childrenFunc"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector Children(Func<SelectorItem, IEnumerable<SelectorItem>> childrenFunc)
        {
            _tree.Children(childrenFunc);
            return this;
        }

        /// <summary>
        ///  根节点信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector Root((string title,string value) source)
        {
            _tree.Root(source);
            return this;
        }

        /// <summary>
        /// 根节点信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector Root(IEnumerable<(string title,string value)> source)
        {
            _tree.Root(source);
            return this;
        }

        /// <summary>
        /// 根节点信息
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector Root(IEnumerable<SelectorItem> source)
        {
            _tree.Root(source);
            return this;
        }

        /// <summary>
        /// 最大输出的层级,默认为4级,-1为不限制
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public PageForm_TreeViewSelector MaxLevel(int level)
        {
            _tree.MaxLevel(level);

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine("<div class=\"input-group dropdown-treeview-container\">");
            writer.WriteLine("<label class=\"form-control\"><span class=\"title\"></span></label>");
            writer.WriteLine("<span class=\"input-group-btn dropdown-treeview-buttongroup\">\r\n<button type=\"button\" class=\"btn btn-success dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"true\">\r\n<span class=\"fa fa-caret-down\"></span>\r\n</button>\r\n</span>");

            writer.WriteLine($"<input type=\"hidden\" name=\"{_name}\" value=\"{_values.JoinToString(",")}\">");

            writer.WriteLine(_tree.Render().ToString());

            writer.WriteLine("</div> <!-- .dropdown-treeview-container -->");
            
        }
    }
}
